

#import "ActionView.h"
#import "JZMTBtnView.h"


@interface ActionView()<UMSocialUIDelegate>
@end
@implementation ActionView
@synthesize CancelButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 180);
//        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 280);
        CGRect baseRect = [[UIScreen mainScreen] bounds];
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
         
            UIView *sheetContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, baseRect.size.width, 180-61)];
//            UIView *sheetContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, baseRect.size.width, 280-61)];
            sheetContentView.backgroundColor = [UIColor whiteColor];
            [self addSubview:sheetContentView];
            
            
            NSArray *titleArray = [NSArray arrayWithObjects:@"微信好友",@"朋友圈",nil];
            NSArray *imageArray = [NSArray arrayWithObjects:@"share_wechat",@"share_wechatfrend",nil];
            
//            NSArray *titleArray = [NSArray arrayWithObjects:@"微信好友",@"朋友圈",@"QQ",@"QQ空间",@"新浪微博",nil];
//            NSArray *imageArray = [NSArray arrayWithObjects:@"share_wechat",@"share_wechatfrend",@"share_QQ",@"share_qqspace",@"share_sina",nil];
            
            CGFloat buttonW = 60;
            CGFloat headermarge;
            if (IS_IPHONE_5 ||IS_IPHONE_4_OR_LESS) {
                headermarge = 25;
            }else{
                headermarge = 40;
            }
            
            CGFloat buttonH = 80;
            CGFloat marge = (SCREEN_WIDTH-(3*buttonW)-(headermarge*2))/2;
            
            for (int i = 0 ; i < titleArray.count; i++) {
                NSInteger index = i % 3;
                NSInteger page = i / 3;
                // 圆角按钮
                CGRect rect = CGRectMake(index * (buttonW+marge)+headermarge,(page  * 95)+15, buttonW, buttonH);
                JZMTBtnView *btnView = [[JZMTBtnView alloc] initWithFrame:rect];
                btnView.imageStr = imageArray[i];
                btnView.titleStr = titleArray[i];
                btnView.titleColor = [UIColor colorWithHexString:@"525252"];
                btnView.font = [UIFont systemFontOfSize:13];
                btnView.tag = i;
                [sheetContentView addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareButton:)];
                [btnView addGestureRecognizer:tap];
            }
        }

        self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
       
        CancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:CancelButton];
        [CancelButton makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).with.offset(0);
            make.right.equalTo(self).with.offset(0);
            make.left.equalTo(self).with.offset(0);
            make.height.mas_equalTo(@60);
        }];
        CancelButton.backgroundColor = [UIColor whiteColor];
        CancelButton.titleLabel.font = [UIFont systemFontOfSize:18];
        [CancelButton setTitleColor:[UIColor colorWithHexString:@"03ab9e"] forState:UIControlStateNormal];
        [CancelButton setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
        [CancelButton setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateHighlighted];
        [CancelButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIView *lineView = [[UIView alloc]init];
        [self addSubview:lineView];
        [lineView makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(CancelButton.top).with.offset(0);
            make.right.equalTo(self).with.offset(-20);
            make.left.equalTo(self).with.offset(20);
            make.height.mas_equalTo(@1);
        }];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#ebebeb"];
    
        self.transparentView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.transparentView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
        [self.transparentView addTarget:self
                         action:@selector(dismiss)
               forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)setPid:(NSString *)pid{
    _pid = pid;
}

- (void)shareButton:(UITapGestureRecognizer *)sender{
    
    [self dismiss];
    ProgressHUDStr;
    
    NSInteger tag = sender.view.tag;
    NSString *strType = nil;
    switch (tag) {
        case 0:
            strType = @"0";
            break;
        case 1:
            strType = @"1";
            break;
        case 2:
            strType = @"2";
            break;
        case 3:
            strType = @"2";
            break;
        case 4:
            strType = @"3";
            break;
        default:
            break;
    }
    if (self.isweb == 2) {//食事周刊
        [NetworkManager requestIndexGetweeklylistnewsid:self.pid type:strType success:^(NSDictionary *dictResponse) {
            ProgressDismiss;
            [self shareinfoNSDictionary:dictResponse andindex:sender.view.tag];
        } failure:^(NSError *error) {
            ProgressDismiss;
        }];
    }else if(self.isweb ==1){//文章
        [NetworkManager requestIndexGetShareofnewsWithNewsid:self.pid type:strType success:^(NSDictionary *dictResponse) {
            ProgressDismiss;
            [self shareinfoNSDictionary:dictResponse andindex:sender.view.tag];
        } failure:^(NSError *error) {
            ProgressDismiss;
        }];
    }else if (self.isweb ==3){//小卫士分享
        NSLog(@"小卫士分享");
        [NetworkManager requestAcsuzhouXwaGetSharemid:UserModel.mid password:UserModel.password pid:self.pid type:strType  success:^(NSDictionary *dictResponse) {
            ProgressDismiss;
            [self shareinfoNSDictionary:dictResponse andindex:sender.view.tag];
        } failure:^(NSError *error) {
            ProgressDismiss;
        }];
    }else if (self.isweb==6){//"我的"推荐给好友
        [NetworkManager requestMemberShareFromAppWithsuccess:^(NSDictionary *dictResponse) {
            ProgressDismiss;
            [self shareinfoNSDictionary:dictResponse andindex:sender.view.tag];
        } failure:^(NSError *error) {
            ProgressDismiss;
        }];
    }
    else{//项目分享
        [NetworkManager requestFoundingGetShareWithMid:UserModel.mid password:UserModel.password fdid:self.pid type:strType success:^(NSDictionary *dictResponse) {
            ProgressDismiss;
            [self shareinfoNSDictionary:dictResponse andindex:sender.view.tag];
        } failure:^(NSError *error) {
            
        }];
    }
}

-(void)shareinfoNSDictionary:(NSDictionary *)dictResponse andindex:(NSInteger)index{
    
    NSString * title = [NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"title"]];
    NSString *description = [NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"desc"]];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"imgUrl"]]]];
    UIImage *urlImage = [UIImage imageWithData:data];
    NSString *webpageUrl = [NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"link"]];;
    
    NSLog(@"title=%@,description = %@,webpageurl = %@",title,description,webpageUrl);
    
    NSString *UMShareToString;
    if (index==0) {
        UMShareToString =UMShareToWechatSession;
    }else if (index==1){
        UMShareToString =UMShareToWechatTimeline;
    }else if (index==2){
        UMShareToString =UMShareToQQ;
    }else if (index==3){
        UMShareToString =UMShareToQzone;
    }else{
        UMShareToString =UMShareToSina;
    }
    [UMSocialData defaultData].extConfig.qqData.url = webpageUrl;
    [UMSocialData defaultData].extConfig.qqData.url = webpageUrl;
    [UMSocialData defaultData].extConfig.wechatSessionData.url = webpageUrl;
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = webpageUrl;
    
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToString] content:title image:urlImage location:nil urlResource:nil presentedController:[UIViewController new] completion:^(UMSocialResponseEntity *shareResponse){
        if (shareResponse.responseCode == UMSResponseCodeSuccess) {
            NSLog(@"分享成功！");
            
            [NetworkManager requestIndexGetSharefinishWithmid:UserModel.mid password:UserModel.password newsid:self.pid success:^(NSDictionary *dictResponse) {
            } failure:^(NSError *error) {
            }];
        }
    }];
}


- (void)removeFromView {
        [UIView animateWithDuration:0.2f
                              delay:0.0f
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^() {
                             self.transparentView.alpha = 0.0f;
                             self.center = CGPointMake(CGRectGetWidth(self.frame) / 2.0, CGRectGetHeight([UIScreen mainScreen].bounds) + CGRectGetHeight(self.frame) / 2.0);
                         } completion:^(BOOL finished) {
                             [self.transparentView removeFromSuperview];
                             [self removeFromSuperview];
                             self.visible = NO;
                         }];
    
}

- (void)showInView:(UIView *)theView {
    
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:self.transparentView];
    [keywindow addSubview:self];
    
    CGRect theScreenRect = [UIScreen mainScreen].bounds;
    
    float height;
    float x;
    
    if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
        height = CGRectGetHeight(theScreenRect);
        x = CGRectGetWidth(theView.frame) / 2.0;
    } else {
        height = CGRectGetWidth(theScreenRect);
        x = CGRectGetHeight(theView.frame) / 2.0;

    }
    
    self.center = CGPointMake(x, height + CGRectGetHeight(self.frame) / 2.0);
    [UIView animateWithDuration:0.3f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^() {
                         if (IOS7_OR_LATER) {
                              self.center = CGPointMake(x, height - CGRectGetHeight(self.frame) / 2.0);
                         } else {
                              self.center = CGPointMake(x, (height - 20) - CGRectGetHeight(self.frame) / 2.0);
                         }
                         
                     } completion:^(BOOL finished) {
                         self.visible = YES;
                     }];
}


-(void)dismiss
{
    [self removeFromView];
}


@end
